{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "convertible-metadata",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import librosa\n",
    "import matplotlib.pyplot as plt\n",
    "import soundfile as sf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "distributed-dryer",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot2Dmatrix(a):\n",
    "    a = a/np.sum(a,axis=1)[:,None]  # Normalize\n",
    "    plt.pcolor(a)\n",
    "    \n",
    "def onlyplot2Dmatrix(a):\n",
    "    #a = a/np.sum(a,axis=1)[:,None]  # Normalize\n",
    "    plt.pcolor(a)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "positive-biology",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total 60\n",
      "drwxrwxr-x 4 god god  4096 Mar 25 21:34 aimodels\n",
      "-rw-rw-r-- 1 god god    73 Mar 25 22:31 Aptfile\n",
      "drwxrwxr-x 2 god god  4096 Mar 25 21:34 docs\n",
      "-rw-rw-r-- 1 god god  1083 Mar 25 21:34 LICENSE.md\n",
      "-rw-rw-r-- 1 god god 13740 Mar 26 15:29 main.py\n",
      "drwxrwxr-x 3 god god  4096 Mar 26 16:08 notebooks\n",
      "-rw-rw-r-- 1 god god  2811 Mar 25 22:51 README.md\n",
      "-rw-rw-r-- 1 god god    36 Mar 26 12:39 requirements-dev.txt\n",
      "-rw-rw-r-- 1 god god   290 Mar 25 22:14 requirements.txt\n",
      "-rw-rw-r-- 1 god god  1151 Mar 26 15:46 ROADMAP.md\n",
      "drwxrwxr-x 2 god god  4096 Mar 26 16:20 temporal\n",
      "drwxrwxr-x 2 god god  4096 Mar 26 12:27 tools_example\n"
     ]
    }
   ],
   "source": [
    "!ls -l ../"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "alien-religious",
   "metadata": {},
   "outputs": [],
   "source": [
    "def audio_to_audio_frame_stack(sound_data, frame_length, hop_length_frame):\n",
    "    \"\"\"This function take an audio and split into several frame\n",
    "       in a numpy matrix of size (nb_frame,frame_length)\"\"\"\n",
    "\n",
    "    sequence_sample_length = sound_data.shape[0]\n",
    "    print(\"\\n[audio_to_audio_frame_stack][sound_data]\")\n",
    "    print(sound_data)\n",
    "    print(\"len(sound_data)\",len(sound_data))\n",
    "    print(\"type:\",type(sound_data))\n",
    "    \n",
    "    sound_data_list = [sound_data[start:start + frame_length] for start in range(0, sequence_sample_length - frame_length + 1, hop_length_frame)]  # get sliding windows\n",
    "    print(\"\\n[audio_to_audio_frame_stack][sound_data_list]\")\n",
    "    print(sound_data_list)\n",
    "    print(\"len(sound_data_list[0])\",len(sound_data_list[0]))\n",
    "    print(\"type:\",type(sound_data_list[0]))\n",
    "    sound_data_array = np.vstack(sound_data_list)\n",
    "    \n",
    "    print(\"\\n[audio_to_audio_frame_stack][sound_data_array]\")\n",
    "    print(sound_data_array)\n",
    "    print(\"len(sound_data_array[0])\",len(sound_data_array[0]))\n",
    "    print(\"type:\",type(sound_data_array[0]))\n",
    "    \n",
    "    if sound_data_array[0].all()==np.asarray(sound_data_list[0]).all():\n",
    "        print(\"\\nIt's the same\")\n",
    "    \n",
    "    return sound_data_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "technical-separate",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('../temporal/input.wav', <http.client.HTTPMessage at 0x7f79581997f0>)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import urllib.request\n",
    "myurl=\"https://mosheman5.github.io/DNP/wav_files/noisy_samples/p232_005.wav\"\n",
    "urllib.request.urlretrieve(myurl, filename=\"../temporal/input.wav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "active-domain",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------\n",
      "[-0.01806641 -0.04641724 -0.02282715 ... -0.0451355  -0.04025269\n",
      " -0.0569458 ]\n",
      "LEN: 99946\n",
      "------------------------------------------------\n",
      "\n",
      "[audio_to_audio_frame_stack][sound_data]\n",
      "[-0.01806641 -0.04641724 -0.02282715 ... -0.0451355  -0.04025269\n",
      " -0.0569458 ]\n",
      "len(sound_data) 99946\n",
      "type: <class 'numpy.ndarray'>\n",
      "\n",
      "[audio_to_audio_frame_stack][sound_data_list]\n",
      "[array([-0.01806641, -0.04641724, -0.02282715, ..., -0.12792969,\n",
      "       -0.13275146, -0.12677002], dtype=float32), array([-0.11730957, -0.10400391, -0.08511353, ..., -0.01235962,\n",
      "        0.00823975,  0.03045654], dtype=float32), array([ 0.05194092,  0.06314087,  0.07098389, ..., -0.03244019,\n",
      "        0.01654053,  0.02807617], dtype=float32), array([0.01446533, 0.01953125, 0.06155396, ..., 0.14749146, 0.14730835,\n",
      "       0.15335083], dtype=float32), array([0.1479187 , 0.13034058, 0.12664795, ..., 0.01794434, 0.01104736,\n",
      "       0.00439453], dtype=float32), array([ 0.00143433, -0.00289917, -0.00524902, ...,  0.12213135,\n",
      "        0.10943604,  0.11022949], dtype=float32), array([0.10244751, 0.09310913, 0.08721924, ..., 0.03726196, 0.03515625,\n",
      "       0.04156494], dtype=float32), array([0.04449463, 0.04360962, 0.0451355 , ..., 0.03100586, 0.0345459 ,\n",
      "       0.03848267], dtype=float32), array([ 0.03604126,  0.03381348,  0.0368042 , ..., -0.01290894,\n",
      "       -0.04342651, -0.01147461], dtype=float32), array([-0.00524902, -0.02590942, -0.04754639, ...,  0.04666138,\n",
      "        0.05841064,  0.07394409], dtype=float32), array([ 0.08673096,  0.11392212,  0.12106323, ..., -0.00549316,\n",
      "        0.00030518,  0.01156616], dtype=float32), array([ 0.0295105 ,  0.04772949,  0.07070923, ..., -0.09109497,\n",
      "       -0.08569336, -0.0866394 ], dtype=float32)]\n",
      "len(sound_data_list[0]) 8064\n",
      "type: <class 'numpy.ndarray'>\n",
      "\n",
      "[audio_to_audio_frame_stack][sound_data_array]\n",
      "[[-0.01806641 -0.04641724 -0.02282715 ... -0.12792969 -0.13275146\n",
      "  -0.12677002]\n",
      " [-0.11730957 -0.10400391 -0.08511353 ... -0.01235962  0.00823975\n",
      "   0.03045654]\n",
      " [ 0.05194092  0.06314087  0.07098389 ... -0.03244019  0.01654053\n",
      "   0.02807617]\n",
      " ...\n",
      " [-0.00524902 -0.02590942 -0.04754639 ...  0.04666138  0.05841064\n",
      "   0.07394409]\n",
      " [ 0.08673096  0.11392212  0.12106323 ... -0.00549316  0.00030518\n",
      "   0.01156616]\n",
      " [ 0.0295105   0.04772949  0.07070923 ... -0.09109497 -0.08569336\n",
      "  -0.0866394 ]]\n",
      "len(sound_data_array[0]) 8064\n",
      "type: <class 'numpy.ndarray'>\n",
      "\n",
      "It's the same\n"
     ]
    }
   ],
   "source": [
    "list_sound_array = []\n",
    "\n",
    "# open the audio file\n",
    "y, sr = librosa.load('../temporal/input.wav', sr=None)\n",
    "print(\"------------------------------------------------\")\n",
    "print(y)\n",
    "print('LEN:',len(y))\n",
    "print(\"------------------------------------------------\")\n",
    "total_duration = librosa.get_duration(y=y, sr=sr)\n",
    "\n",
    "if (total_duration >= 1.0):\n",
    "    list_sound_array.append(audio_to_audio_frame_stack(sound_data=y, frame_length=8064, hop_length_frame=8064))\n",
    "else:\n",
    "    print(f\"The following file {os.path.join(audio_dir,file)} is below the min duration\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "chief-method",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16000"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "eight-selling",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[-0.01806641, -0.04641724, -0.02282715, ..., -0.12792969,\n",
       "         -0.13275146, -0.12677002],\n",
       "        [-0.11730957, -0.10400391, -0.08511353, ..., -0.01235962,\n",
       "          0.00823975,  0.03045654],\n",
       "        [ 0.05194092,  0.06314087,  0.07098389, ..., -0.03244019,\n",
       "          0.01654053,  0.02807617],\n",
       "        ...,\n",
       "        [-0.00524902, -0.02590942, -0.04754639, ...,  0.04666138,\n",
       "          0.05841064,  0.07394409],\n",
       "        [ 0.08673096,  0.11392212,  0.12106323, ..., -0.00549316,\n",
       "          0.00030518,  0.01156616],\n",
       "        [ 0.0295105 ,  0.04772949,  0.07070923, ..., -0.09109497,\n",
       "         -0.08569336, -0.0866394 ]], dtype=float32)]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list_sound_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "another-polish",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.01806641 -0.04641724 -0.02282715 ... -0.12792969 -0.13275146\n",
      "  -0.12677002]\n",
      " [-0.11730957 -0.10400391 -0.08511353 ... -0.01235962  0.00823975\n",
      "   0.03045654]\n",
      " [ 0.05194092  0.06314087  0.07098389 ... -0.03244019  0.01654053\n",
      "   0.02807617]\n",
      " ...\n",
      " [-0.00524902 -0.02590942 -0.04754639 ...  0.04666138  0.05841064\n",
      "   0.07394409]\n",
      " [ 0.08673096  0.11392212  0.12106323 ... -0.00549316  0.00030518\n",
      "   0.01156616]\n",
      " [ 0.0295105   0.04772949  0.07070923 ... -0.09109497 -0.08569336\n",
      "  -0.0866394 ]]\n",
      "(12, 8064)\n"
     ]
    }
   ],
   "source": [
    "audio_data=np.vstack(list_sound_array)\n",
    "print(audio_data)\n",
    "print(audio_data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "solar-cursor",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.01806641 -0.04641724 -0.02282715 ... -0.12792969 -0.13275146\n",
      "  -0.12677002]\n",
      " [-0.11730957 -0.10400391 -0.08511353 ... -0.01235962  0.00823975\n",
      "   0.03045654]\n",
      " [ 0.05194092  0.06314087  0.07098389 ... -0.03244019  0.01654053\n",
      "   0.02807617]\n",
      " ...\n",
      " [-0.00524902 -0.02590942 -0.04754639 ...  0.04666138  0.05841064\n",
      "   0.07394409]\n",
      " [ 0.08673096  0.11392212  0.12106323 ... -0.00549316  0.00030518\n",
      "   0.01156616]\n",
      " [ 0.0295105   0.04772949  0.07070923 ... -0.09109497 -0.08569336\n",
      "  -0.0866394 ]]\n",
      "(12, 8064)\n"
     ]
    }
   ],
   "source": [
    "print(list_sound_array[0])\n",
    "print(list_sound_array[0].shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "pretty-assistant",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "It's the same\n"
     ]
    }
   ],
   "source": [
    "if list_sound_array[0].all()==audio_data.all():\n",
    "    print(\"It's the same\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "cloudy-australian",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.01806641, -0.04641724, -0.02282715, ..., -0.12792969,\n",
       "        -0.13275146, -0.12677002],\n",
       "       [-0.11730957, -0.10400391, -0.08511353, ..., -0.01235962,\n",
       "         0.00823975,  0.03045654],\n",
       "       [ 0.05194092,  0.06314087,  0.07098389, ..., -0.03244019,\n",
       "         0.01654053,  0.02807617],\n",
       "       ...,\n",
       "       [-0.00524902, -0.02590942, -0.04754639, ...,  0.04666138,\n",
       "         0.05841064,  0.07394409],\n",
       "       [ 0.08673096,  0.11392212,  0.12106323, ..., -0.00549316,\n",
       "         0.00030518,  0.01156616],\n",
       "       [ 0.0295105 ,  0.04772949,  0.07070923, ..., -0.09109497,\n",
       "        -0.08569336, -0.0866394 ]], dtype=float32)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "audio_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "casual-knight",
   "metadata": {},
   "outputs": [],
   "source": [
    "sf.write('../temporal/output_audio_data.wav',np.hstack(audio_data),samplerate=8000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "allied-composition",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8064"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(audio_data[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "external-virginia",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.01806641, -0.04641724, -0.02282715, ..., -0.0451355 ,\n",
       "       -0.04025269, -0.0569458 ], dtype=float32)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "genuine-night",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8064"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "audio_data[0].shape[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "experienced-tradition",
   "metadata": {},
   "source": [
    "### ------------------\n",
    "### 2nd method\n",
    "### ------------------\n",
    "loading as array and hstack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "former-concentration",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "It's the same\n"
     ]
    }
   ],
   "source": [
    "if audio_data[0].all()==y.all():\n",
    "    print(\"It's the same\")\n",
    "else:\n",
    "    print(\"It's not the same\")\n",
    "    print('audio_data[0] len: \\t',len(audio_data[0]))\n",
    "    print('y librosa len: \\t\\t',len(y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "stable-weight",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([1, 2]), array([3, 4]), array([5, 6]), array([7, 8]), array([ 9, 10])]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=np.asarray([1 ,2, 3, 4, 5, 6, 7, 8, 9, 10])\n",
    "a=2 # frame_length\n",
    "b=a # hop_length_frame\n",
    "y = [x[start:start + a] for start in range(0, len(x) - a + 1, b)] \n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "portable-church",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[array([1, 2]), array([3, 4]), array([5, 6]), array([7, 8]), array([ 9, 10])]]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list_sound_array = []\n",
    "\n",
    "list_sound_array.append(y)\n",
    "list_sound_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "editorial-elimination",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2],\n",
       "       [ 3,  4],\n",
       "       [ 5,  6],\n",
       "       [ 7,  8],\n",
       "       [ 9, 10]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack(list_sound_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "agreed-growth",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([ 0.01535975,  0.01535975,  0.01535975,  1.2       , -3.2       ]),\n",
       " array([ 3.2,  3.2, -3.2,  3.2,  3.2]),\n",
       " array([ 8. ,  3.2,  3.2,  3.2, 11. ]),\n",
       " array([0. , 3.2, 3.2, 3.2, 3.2]),\n",
       " array([3.2, 3.2, 3.2, 1. , 2. ])]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myaudio=[[0.01535975], [0.01535975],[0.01535975],[1.2],[-3.2],[3.2],[3.2],[-3.2],[3.2],[3.2],[8],[3.2],[3.2],[3.2],[11],[0],[3.2],[3.2],[3.2],[3.2],[3.2],[3.2],[3.2],[1],[2],[3],]\n",
    "myaudio=np.hstack(myaudio)\n",
    "a=5 # frame_length\n",
    "b=a # hop_length_frame\n",
    "y = [myaudio[start:start + a] for start in range(0, len(myaudio) - a + 1, b)] \n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "stone-minister",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(myaudio)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "polyphonic-studio",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.01535975,  0.01535975,  0.01535975,  1.2       , -3.2       ],\n",
       "       [ 3.2       ,  3.2       , -3.2       ,  3.2       ,  3.2       ],\n",
       "       [ 8.        ,  3.2       ,  3.2       ,  3.2       , 11.        ],\n",
       "       [ 0.        ,  3.2       ,  3.2       ,  3.2       ,  3.2       ],\n",
       "       [ 3.2       ,  3.2       ,  3.2       ,  1.        ,  2.        ]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y=np.vstack(y)\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "pacific-elite",
   "metadata": {},
   "source": [
    "### 3rd method\n",
    "\n",
    "resampling by librosa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "working-tennis",
   "metadata": {},
   "outputs": [],
   "source": [
    "import librosa\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "systematic-canal",
   "metadata": {},
   "outputs": [],
   "source": [
    "#myaudio=[[0.01535975], [0.01535975],[0.01535975],[1.2],[-3.2],[3.2],[3.2],[-3.2],[3.2],[3.2],[8],[3.2],[3.2],[3.2],[11],[0],[3.2],[3.2],[3.2],[3.2],[3.2],[3.2],[3.2],[1],[2],[3],]\n",
    "myaudio=audio_data\n",
    "mydata=np.hstack(myaudio)\n",
    "mydata_resampled=librosa.resample(mydata, \n",
    "                                  orig_sr=16000, \n",
    "                                  target_sr=8000, \n",
    "                                  res_type='kaiser_best')\n",
    "sf.write('../temporal/output_audio_data_3rd.wav',np.hstack(mydata_resampled),samplerate=8000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "desperate-instrument",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SHAPE ORIGINAL (96768,)\n",
      "SHAPE RESAMPLED (48384,)\n"
     ]
    }
   ],
   "source": [
    "print('SHAPE ORIGINAL',np.hstack(myaudio).shape)\n",
    "print('SHAPE RESAMPLED',mydata_resampled.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "dying-amplifier",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "12*8064/48384"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "civilian-belize",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "a=8064 # frame_length\n",
    "b=8064 # hop_length_frame\n",
    "y = [mydata_resampled[start:start + a] for start in range(0, len(mydata_resampled) - a + 1, b)] \n",
    "y_original = [mydata[start:start + a] for start in range(0, len(mydata) - a + 1, b)] \n",
    "audio=np.asarray(y)\n",
    "audio_original=np.asarray(y_original)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "biological-letters",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "48384"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(np.hstack(audio))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "pursuant-macro",
   "metadata": {},
   "outputs": [],
   "source": [
    "myaudio=[[0.01535975], [0.01535975],[0.01535975],[1.2],[-3.2],[3.2],[3.2],[-3.2],[3.2],[3.2],[8],[3.2],[3.2],[3.2],[11],[0],[3.2],[3.2],[3.2],[3.2],[3.2],[3.2],[3.2],[1],[2],[3],]\n",
    "myaudio=np.asarray(myaudio)\n",
    "\n",
    "myaudio_hstack=np.hstack(myaudio)\n",
    "mydata_resampled=librosa.resample(myaudio_hstack.T, \n",
    "                                  orig_sr=4100, \n",
    "                                  target_sr=8000, \n",
    "                                  res_type='kaiser_best')\n",
    "\n",
    "a=2 # frame_length\n",
    "b=2 # hop_length_frame\n",
    "y = [mydata_resampled[start:start + a] for start in range(0, len(mydata_resampled) - a + 1, b)] \n",
    "y_original = [mydata[start:start + a] for start in range(0, len(mydata) - a + 1, b)] \n",
    "audio=np.asarray(y)\n",
    "audio_original=np.asarray(y_original)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "meaningful-cancellation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.01535975],\n",
       "       [ 0.01535975],\n",
       "       [ 0.01535975],\n",
       "       [ 1.2       ],\n",
       "       [-3.2       ],\n",
       "       [ 3.2       ],\n",
       "       [ 3.2       ],\n",
       "       [-3.2       ],\n",
       "       [ 3.2       ],\n",
       "       [ 3.2       ],\n",
       "       [ 8.        ],\n",
       "       [ 3.2       ],\n",
       "       [ 3.2       ],\n",
       "       [ 3.2       ],\n",
       "       [11.        ],\n",
       "       [ 0.        ],\n",
       "       [ 3.2       ],\n",
       "       [ 3.2       ],\n",
       "       [ 3.2       ],\n",
       "       [ 3.2       ],\n",
       "       [ 3.2       ],\n",
       "       [ 3.2       ],\n",
       "       [ 3.2       ],\n",
       "       [ 1.        ],\n",
       "       [ 2.        ],\n",
       "       [ 3.        ]])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myaudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "assigned-asset",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([-0.19597516,  0.47883534]),\n",
       " array([ 0.25188036, -0.64777505]),\n",
       " array([-0.19645763,  1.53549938]),\n",
       " array([ 1.36890697, -1.7880331 ]),\n",
       " array([-3.70056618, -0.44700419]),\n",
       " array([4.84424077, 5.5553988 ]),\n",
       " array([ 1.00071586, -2.73075072]),\n",
       " array([-1.67238745,  1.38655853]),\n",
       " array([2.71376123, 3.16675322]),\n",
       " array([4.97616212, 6.96055982]),\n",
       " array([6.70644449, 4.83266796]),\n",
       " array([3.46962062, 2.67455806]),\n",
       " array([1.91378052, 2.92316362]),\n",
       " array([ 6.84176475, 10.09302307]),\n",
       " array([8.02571738, 2.2103903 ]),\n",
       " array([-0.80243119,  1.51199441]),\n",
       " array([4.64314411, 4.35252553]),\n",
       " array([2.3653327 , 2.28452797]),\n",
       " array([3.7238782 , 3.96686637]),\n",
       " array([2.88109867, 2.54037949]),\n",
       " array([3.34889971, 3.78033654]),\n",
       " array([3.27120968, 2.64934775]),\n",
       " array([2.13503151, 1.34081162]),\n",
       " array([0.92311349, 1.87744936]),\n",
       " array([3.28879205, 3.05891209])]"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "indirect-commitment",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([0.01535975, 0.01535975]),\n",
       " array([0.01535975, 1.2       ]),\n",
       " array([-3.2,  3.2]),\n",
       " array([ 3.2, -3.2]),\n",
       " array([3.2, 3.2]),\n",
       " array([8. , 3.2]),\n",
       " array([3.2, 3.2]),\n",
       " array([11.,  0.]),\n",
       " array([3.2, 3.2]),\n",
       " array([3.2, 3.2]),\n",
       " array([3.2, 3.2]),\n",
       " array([3.2, 1. ]),\n",
       " array([2., 3.])]"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_original"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "criminal-complement",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'T'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-83-5ac7fbf1e7a6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my_original\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'list' object has no attribute 'T'"
     ]
    }
   ],
   "source": [
    "np.vstack(y_original.T)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
